# Coefficient that varies on domain

**URL:** <https://community.freefem.org/t/coefficient-that-varies-on-domain/2647>\
**Category:** General Discussion\
**Created:** [August 7, 2023, 11:35pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647 "2023-08-07T23:35:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [August 7, 2023, 11:35pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/1 "2023-08-07T23:35:29Z")

</div>

Hello, I’m trying to implement a coefficient value that varies depending on the area of the domain, this is the definition

```auto
//parameters
func real kappa() {
 int ni = 9, nj = 9;
 real r1=1./32.*3./ni, k1=1., k3=1.;
 real r2=1./16.*3./ni, k2=10000.0;

 for(int j=0; j<(nj-1); j++){
  real yj=(2.*j+1.)/(2.*nj);
  for(int i=0; i<(ni-1); i++){
   real xi=(2.*i+1.)/(2.*ni);
   if( (x>=(xi-r1)) && (x<=(xi+r1)) && (y>=(yj-r1)) && (y<=(yj+r1)))
    return k1;
   if ( (x>=(xi-r2)) && (x<=(xi+r2)) && (y>=(yj-r2)) && (y<=(yj+r2)))
    return k2;
  }
 }
return k3;

}

```

I need this coefficient for the integral for the Darcy Model

```auto
   varf ahK(etaK, varphi)
    = int2d(Th[K])(kappa*grad(etaK)'*grad(varphi));

```

but I’m getting the following error

```auto
varf ahK(etaK, varphi)
  289 : = int2d(Th[K])(kappa*grad(etaK) [dx(etaK), dy(etaK)]'* error operator * <11Polymorphic>, <10LinearCombI7MGauche4C_F0E>

```

can someone help me?

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [August 8, 2023, 2:34pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/2 "2023-08-08T14:34:44Z")

</div>

this is an example of the code I want to implement on FreeFem++

```auto
local function func_kappa(x,y)
	ni=27; nj=27;
	-- (not so) aligned to the mesh
	r1=1./32.*3./ni ; k1=1.; k3=1.; -- radius and kappa of the inner squared cell
	r2=1./16.*3./ni ; k2=10000.0; -- radius and kappa of the outer square cell

	for j=0,(nj-1) do
		yj=(2.*j+1.)/(2.*nj);
		for i=0,(ni-1) do
			xi=(2.*i+1.)/(2.*ni);
			if ( (x>=(xi-r1)) and (x<=(xi+r1)) and (y>=(yj-r1)) and (y<=(yj+r1))) then
				return k1;
			end
			if ( (x>=(xi-r2)) and (x<=(xi+r2)) and (y>=(yj-r2)) and (y<=(yj+r2))) then
				return k2;
			end
		end
	end
	return k3; -- kappa of the embedding matrix
end

kappa = func_kappa,

```

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [August 8, 2023, 3:56pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/3 "2023-08-08T15:56:28Z")

</div>

Did that work? If not can you upload your entire code?

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [August 8, 2023, 6:04pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/4 "2023-08-08T18:04:21Z")

</div>

The technics is very slow, you function kappa is defined by  
with a Gh grid (x\_i,y\_i) of size ni, nj you want

let us call dist00(x,y) is the minimal distance of (x,y) the the point of the grid in norme infini  
now the fonction is just k3+(dist00(x,y) \< r1)_(k1-k3) + (dist00(x,y) \< r2)_(k2-k1-k3)

the trick to code dist00(x,y) arrive to morrow!

because

---

<div class="post-metadata">

**Author:** ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)\
**Post date:** [August 8, 2023, 6:16pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/5 "2023-08-08T18:16:34Z")

</div>

It worked like this:

```auto
//parameters
func real varkappa(int in, int jn){
  int ni = in, nj = jn;
  real r1=1./32.*3./ni, k1=1., k3=1.;
  real r2=1./16.*3./ni, k2=10000.0;

  for(int j=0; j<(nj-1); j++){
    real yj=(2.*j+1.)/(2.*nj);
    for(int i=0; i<(ni-1); i++){
      real xi=(2.*i+1.)/(2.*ni);
      if( (x>=(xi-r1)) && (x<=(xi+r1)) && (y>=(yj-r1)) && (y<=(yj+r1))){
        return k1;
      }
      if ( (x>=(xi-r2)) && (x<=(xi+r2)) && (y>=(yj-r2)) && (y<=(yj+r2))){
        return k2;
      }
    }
  }
  return k3;
}

real kappa = varkappa(9,9);

```

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [August 10, 2023, 4:19pm UTC](https://community.freefem.org/t/coefficient-that-varies-on-domain/2647/6 "2023-08-10T16:19:24Z")

</div>

so my final version (the complexity of function Kappa is O(1) not O(ni\*nj)

[yyy.edp](https://community.freefem.org/uploads/short-url/jhT6EQMdJR2sY0lPUFkpz6RcIB.edp) (1.1 KB)
